package com.leetcode面试题.no17_11;

public class Solution {
    public int findClosest(String[] words, String word1, String word2) {
        int min = -1;
        int lastWord1Index = -1;
        int lastWord2Index = -1;

        for (int i = 0; i < words.length - 1; i++) {
            if (words[i].equals(word1)) {
                lastWord1Index = i;
            }
            if (words[i].equals(word2)) {
                lastWord2Index = i;
            }

            if (lastWord1Index == -1 || lastWord2Index == -1) {
                continue;
            }

//            int tmp = lastWord2Index > lastWord1Index ? lastWord2Index - lastWord1Index
//                    : lastWord1Index - lastWord2Index;
            int tmp = Math.abs(lastWord1Index - lastWord2Index);

            // 更新
            if (tmp < min || min == -1) {
                min = tmp;
            }
        }

        return min;
    }
}
